<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="商品名称">
                <a-input placeholder="请输入商品名称" v-model="queryParam.name" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="选择门店">
                <a-select v-model="queryParam.md_id">
                  <a-select-option :value="item.id" v-for="item in allShop" :key="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="">
                <a-button type="primary" @click="getGoodsList">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :bordered="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getGoodsList">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="1"> 已上架 </a-radio-button>
          <a-radio-button value="0"> 已下架 </a-radio-button>
        </a-radio-group>
      </div>
      <a-table
        :columns="columns"
        :data-source="goodsList"
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :rowKey="
          (r, i) => {
            return i.toString()
          }
        "
      >
        <div slot="good" slot-scope="scope">
          <div class="name">
            <div class="pic">
              <img :src="scope.cover_pic" alt="" />
            </div>
            <div class="info">
              <span>{{ scope.name }}</span>
              <span>{{ scope.cat ? scope.cat.map((item) => item.name).join() : '暂无分类' }}</span>
            </div>
          </div>
        </div>
        <span slot="price" slot-scope="text"> ￥{{ text }} </span>
        <span slot="md_price" slot-scope="text"> ￥{{ text }} </span>
        <div slot="status" slot-scope="scope">
          <div class="status">
            <span :class="scope.status == 0 ? 'grey' : 'green'"></span>
            <span>{{ scope.status == 0 ? '已下架' : '上架中' }}</span>
          </div>
        </div>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { outletGoods } from '@/api/goods/goods'
import { allShop } from '@/api/outlet'
import { STable } from '@/components'
const columns = [
  {
    title: '商品名称',
    scopedSlots: {
      customRender: 'good',
    },
    width: 280,
  },
  {
    title: '售价',
    dataIndex: 'price',
    scopedSlots: {
      customRender: 'price',
    },
    sorter: (a, b) => a.price - b.price,
  },
  {
    title: '独立售价',
    dataIndex: 'md_price',
    scopedSlots: {
      customRender: 'md_price',
    },
    sorter: (a, b) => a.md_price - b.md_price,
  },
  {
    title: '库存',
    dataIndex: 'goods_num',
    sorter: (a, b) => a.goods_num - b.goods_num,
  },
  {
    title: '独立库存',
    dataIndex: 'md_goods_num',
    sorter: (a, b) => a.md_goods_num - b.md_goods_num,
  },
  {
    title: '商品状态',
    scopedSlots: {
      customRender: 'status',
    },
  },
]

export default {
  name: 'StaffList',
  components: {
    STable,
  },
  data() {
    return {
      // 表头
      columns,
      // 查询条件参数
      queryParam: {
        status: '-1',
        pageNo: 1,
        pageSize: 10,
        md_id: null,
        name: null,
      },
      // 数据总条数
      totalCount: 0,
      // 所有门店列表
      allShop: [],
      // 商品列表
      goodsList: [],
    }
  },
  created() {
    this.getAllShop()
  },
  methods: {
    // 重置查询参数
    resetQueryParams() {
      Object.assign(this.queryParam, {
        pageNo: 1,
        pageSize: 10,
        md_id: null,
        name: null,
        status: '-1',
      })
      this.getGoodsList()
    },
    // 分页请求
    tablePageChange(e) {
      this.queryParam.pageNo = e
      this.getGoodsList()
    },
    // 获取商品列表
    async getGoodsList() {
      const hide = this.$message.loading('加载中', 0)
      const res = await outletGoods(this.queryParam)
      if (res.code == 0) {
        this.goodsList = res.data.data
        this.totalCount = res.data.totalCount
      }
      hide()
    },
    // 获取所有门店列表
    async getAllShop() {
      const res = await allShop()
      if (res.code == 0) {
        this.allShop = res.data
        if (this.allShop.length) {
          this.queryParam.md_id = this.allShop[0].id
          this.getGoodsList()
        }
      }
    },
  },
}
</script>

<style lang="less" scoped>
.permission-form {
  /deep/ .permission-group {
    margin-top: 0;
    margin-bottom: 0;
  }
}
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-top: 14px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-top: 14px;
    margin-right: 20px;
  }
  .ant-btn {
    margin-top: 14px;
  }
}
.action {
  display: flex;
  align-items: center;
  .line {
    width: 1px;
    height: 14px;
    background-color: #d8d8d8;
    margin-right: 8px;
  }
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}
.status {
  display: flex;
  align-items: center;
  span:nth-child(1) {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 4px;
  }
  .green {
    background-color: #65c817;
  }
  .grey {
    background-color: #c6c6c6;
  }
}
</style>
